package cn.summit.custom.test.service;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import cn.summit.custom.springframework.BeanPostProcessor;
import cn.summit.custom.springframework.annotation.Component;

/**
 * @author summit
 * @since 2022/6/10 11:02
 */
@Component
public class SummitBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        if (beanName.equals("userService")) {
            Object o = Proxy.newProxyInstance(SummitBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("切面==========");
                    return method.invoke(bean, args);
                }
            });
            System.out.println(beanName + "---------");
            return o;
        }
        return bean;
    }
}
